package com.probox.common.entity.service.platfom.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.utils.DateUtils;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.entity.domain.platfom.PlatformStoreGroup;
import com.probox.common.entity.domain.platfom.PlatformStoreTarget;
import com.probox.common.entity.mapper.platfom.PlatformStoreTargetMapper;
import com.probox.common.entity.mapper.system.SysUserMapper;
import com.probox.common.entity.service.platfom.IPlatformStoreGroupService;
import com.probox.common.entity.service.platfom.IPlatformStoreTargetService;
import com.probox.common.entity.vo.centre.order.OrderQueryVo;
import com.probox.systemApi.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 门店目标业绩管理Service业务层处理
 *
 * @author usoft
 * @date 2022-09-24
 */
@Service
@Transactional
public class PlatformStoreTargetServiceImpl extends ServiceImpl<PlatformStoreTargetMapper, PlatformStoreTarget> implements IPlatformStoreTargetService {
    @Autowired
    private PlatformStoreTargetMapper platformStoreTargetMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private IPlatformStoreGroupService platformStoreGroupService;

    @Override
    public PlatformStoreTarget selectTargetByTime(Long storeId) {
        QueryWrapper<PlatformStoreTarget> queryWrapper = Wrappers.query();
        queryWrapper.eq("data_status", DataStatus.valid.getKey());
        queryWrapper.eq("store_id", storeId);
        queryWrapper.last(" order by create_time desc limit 1");
        return platformStoreTargetMapper.selectOne(queryWrapper);
    }

    /**
     * 查询门店目标业绩管理
     *
     * @param targetId 门店目标业绩管理ID
     * @return 门店目标业绩管理
     */
    @Override
    public PlatformStoreTarget selectPlatformStoreTargetById(Long targetId) {
        return this.getById(targetId);
    }

    /**
     * 查询门店目标业绩管理列表
     *
     * @param platformStoreTarget 门店目标业绩管理
     * @return 门店目标业绩管理
     */
    @Override
    public List<PlatformStoreTarget> queryList(PlatformStoreTarget platformStoreTarget) {
        QueryWrapper<PlatformStoreTarget> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(platformStoreTarget.getTargetId())) && platformStoreTarget.getTargetId() != null) {
            queryWrapper.eq("target_id", platformStoreTarget.getTargetId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformStoreTarget.getStoreId())) && platformStoreTarget.getStoreId() != null) {
            queryWrapper.eq("store_id", platformStoreTarget.getStoreId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformStoreTarget.getTargetDate())) && platformStoreTarget.getTargetDate() != null) {
            queryWrapper.eq("target_date", platformStoreTarget.getTargetDate());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformStoreTarget.getHaokaMoney())) && platformStoreTarget.getHaokaMoney() != null) {
            queryWrapper.eq("haoka_money", platformStoreTarget.getHaokaMoney());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformStoreTarget.getHaokaItem())) && platformStoreTarget.getHaokaItem() != null) {
            queryWrapper.eq("haoka_item", platformStoreTarget.getHaokaItem());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformStoreTarget.getHaokaPepole())) && platformStoreTarget.getHaokaPepole() != null) {
            queryWrapper.eq("haoka_pepole", platformStoreTarget.getHaokaPepole());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformStoreTarget.getDataStatus())) && platformStoreTarget.getDataStatus() != null) {
            queryWrapper.eq("data_status", platformStoreTarget.getDataStatus());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformStoreTarget.getCreateUser())) && platformStoreTarget.getCreateUser() != null) {
            queryWrapper.eq("create_user", platformStoreTarget.getCreateUser());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformStoreTarget.getCreateName())) && platformStoreTarget.getCreateName() != null) {
            queryWrapper.eq("create_name", platformStoreTarget.getCreateName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformStoreTarget.getUpdateUser())) && platformStoreTarget.getUpdateUser() != null) {
            queryWrapper.eq("update_user", platformStoreTarget.getUpdateUser());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformStoreTarget.getUpdateName())) && platformStoreTarget.getUpdateName() != null) {
            queryWrapper.eq("update_name", platformStoreTarget.getUpdateName());
        }
        queryWrapper.orderByDesc("target_id");
        return platformStoreTargetMapper.selectList(queryWrapper);
    }

    @Override
    public PlatformStoreTarget sumQueryList(OrderQueryVo queryVo){
        QueryWrapper<PlatformStoreTarget> queryWrapper = Wrappers.query();
        queryWrapper.select("sum(IFNULL(haoka_money,0)) haoka_money,sum(IFNULL(money,0)) money,sum(IFNULL(haoka_item,0)) haoka_item,sum(IFNULL(haoka_pepole,0)) haoka_pepole ");
        queryWrapper.eq("data_Status","1");
        queryWrapper.eq("target_Date", DateUtils.formate(new Date(),"yyyy-MM"));
        if (queryVo.getStoreList() != null){
            queryWrapper.in("store_id",queryVo.getStoreList());
        }else if (queryVo.getStoreId() != null){
            queryWrapper.eq("store_id",queryVo.getStoreId());
        }
        return platformStoreTargetMapper.selectOne(queryWrapper);
    }



    /**
     * 新增门店目标业绩管理
     *
     * @param platformStoreTarget 门店目标业绩管理
     * @return 结果
     */
    @Override
    public boolean insertPlatformStoreTarget(PlatformStoreTarget platformStoreTarget) {


        platformStoreTarget.setCreateUser(SecurityUtils.getUserId());
        platformStoreTarget.setCreateName(SecurityUtils.getUsername());
        platformStoreTarget.setCreateTime(DateUtil.date());

        return this.save(platformStoreTarget);

    }

    /**
     * 修改门店目标业绩管理
     *
     * @param platformStoreTarget 门店目标业绩管理
     * @return 结果
     */
    @Override
    public boolean updatePlatformStoreTarget(PlatformStoreTarget platformStoreTarget) {

        platformStoreTarget.setUpdateUser(SecurityUtils.getUserId());
        platformStoreTarget.setUpdateName(SecurityUtils.getUsername());
        platformStoreTarget.setUpdateTime(DateUtil.date());
        return this.updateById(platformStoreTarget);
    }

    /**
     * 批量删除门店目标业绩管理
     *
     * @param targetIds 需要删除的门店目标业绩管理ID
     * @return 结果
     */
    @Override
    public boolean deletePlatformStoreTargetByIds(Long[] targetIds) {
        UpdateWrapper<PlatformStoreTarget> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("target_id", Arrays.asList(targetIds));
        updateWrapper.set("DATA_STATUS", DataStatus.delete.getKey());
        updateWrapper.set("UPDATE_User", SecurityUtils.getUserId());
        updateWrapper.set("UPDATE_NAME", SecurityUtils.getUsername());
        updateWrapper.set("UPDATE_TIME", DateUtil.date());
        return this.update(updateWrapper);
    }

    @Override
    public List<PlatformStoreTarget> getList(PlatformStoreTarget platformStoreTarget, Integer pageNum, Integer pageSize) {
        /**
         *
         */
        Long storeId = null;
        List<String> arr = new ArrayList<>();
        SysUser sysUser = sysUserMapper.selectUserById(SecurityUtils.getUserId());
        if (ObjectUtil.isNotEmpty(sysUser.getStoreId()) && ObjectUtil.isEmpty(sysUser.getStoreGroupId())
                && ObjectUtil.isEmpty(platformStoreTarget.getStoreId())) {
            storeId = sysUser.getStoreId();
        } else if (ObjectUtil.isNotEmpty(sysUser.getStoreGroupId())) {
            PlatformStoreGroup byId = platformStoreGroupService.getById(sysUser.getStoreGroupId());
            if (ObjectUtil.isNotEmpty(byId)) {
                if (ObjectUtil.isNotEmpty(byId.getStoreIds())) {
                    arr = new ArrayList<>(Arrays.asList(byId.getStoreIds().split(",")));
                }
            }
        }

        PageHelper.startPage(pageNum, pageSize);
        List<PlatformStoreTarget> list = platformStoreTargetMapper.getList(platformStoreTarget.getStartTime(), platformStoreTarget.getEndTime(), storeId, arr);
        return list;
    }


}
